using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using System.Xml;

namespace Barrage.GUI.Elements
{
    /// <summary>
    /// A selectable GUI Element. It can hold some subelements inside it
    /// </summary>
    public abstract class GUISelectableElement : GUIElement
    {
        private const string XML_TABORDER_ATTRIBUTE = "order";

        private int order;
        /// <summary>
        /// The item's order in the layout when using arrows to navigate within the menus
        /// </summary>
        public int Order { get { return order; } }

        int currentSubElement = 0;
        /// <summary>
        /// Gets/Sets the active sub-element
        /// </summary>
        protected int CurrentSubElement { get { return currentSubElement; } set { currentSubElement = Math.Max(0, Math.Min(SubElementsCount - 1, value)); } }
        /// <summary>
        /// Number of sub-elements inside this element (usually 1, override this property to change it)
        /// </summary>
        protected virtual int SubElementsCount { get { return 1; } }

        /// <summary>
        /// Selects the next sub-element
        /// </summary>
        /// <returns>True if the next sub-element was selected, False if there's no more sub-elements</returns>
        public bool SelectNextItem()
        {
            if (currentSubElement < SubElementsCount - 1)
            {
                CurrentSubElement++;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Selects the previous sub-element
        /// </summary>
        /// <returns>True if the previous sub-element was selected, False if there's no more sub-elements</returns>
        public bool SelectPreviousItem()
        {
            if (currentSubElement > 0)
            {
                CurrentSubElement--;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Inits the GUISelectableElement
        /// </summary>
        /// <param name="layout">Layout that holds this item</param>
        /// <param name="template">Template that this item is using</param>
        /// <param name="xn">Xml Node with parameters to this element</param>
        public GUISelectableElement(GUILayout layout, string template, XmlNode xn)
            : base(layout,template,xn)
        {
            try
            {
                this.order = XmlHelper.ParseXMLIntAttribute(xn, XML_TABORDER_ATTRIBUTE, layout.NextFreeIndex);
            }
            catch (Exception)
            {
                this.order = layout.NextFreeIndex;
            }
        }

        /// <summary>
        /// Tells wether this item is currently the selected in the layout or not
        /// </summary>
        public bool Selected { get { return Layout.SelectedItemIndex == order; } }

        /// <summary>
        /// Abstract method. Executes the action associated with user interaction (mouse click, Enter key, gamepad button)
        /// </summary>
        public abstract void Interact();

        
    }
}
